//
//  MSFoodDetailTopView.m
//  MSJD
//
//  Created by xiangbo on 2018/12/12.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSFoodDetailTopView.h"

@implementation MSFoodDetailTopView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews{
    [self addSubview:self.backButton];
    [self addSubview:self.titleLabel];
    [self addSubview:self.collectButton];
    [self addSubview:self.shareButton];
    
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(14);
        make.width.height.mas_equalTo(34);
        make.centerY.equalTo(self);
    }];
    
    [self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(34);
        make.right.equalTo(self.mas_right).offset(-14);
        make.centerY.equalTo(self);
    }];
    
    [self.collectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(34);
        make.right.equalTo(self.shareButton.mas_left);
        make.centerY.equalTo(self);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.height.equalTo(self);
        make.right.equalTo(self.collectButton.mas_left).offset(-5);
    }];
}


#pragma mark - ButtonAction
- (void)backAction:(id)sender{
    if(self.isPresent){
        [[UIViewController currentViewController] dismissViewControllerAnimated:YES completion:nil];
    }else{
        [[UIViewController currentViewController].navigationController popViewControllerAnimated:YES];
    }
}

- (void)collectAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    [self routerEventWithName:kMSCollectAction userInfo:@{kMSCollectAction:[NSNumber numberWithBool:sender.selected]}];
}

- (void)shareAction:(id)sender{
    [self routerEventWithName:kMSShareAction userInfo:nil];
}
#pragma mark - Property
- (UIButton *)backButton{
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont boldSystemFontOfSize:17];
        _titleLabel.textColor = UIColorHex(0x222222);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UIButton *)collectButton{
    if (!_collectButton) {
        _collectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_collectButton setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
        [_collectButton setImage:[UIImage imageNamed:@"like_fill"] forState:UIControlStateSelected];
        [_collectButton addTarget:self action:@selector(collectAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _collectButton;
}

- (UIButton *)shareButton{
    if (!_shareButton) {
        _shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shareButton setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
        [_shareButton addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _shareButton;
}
@end
